package fiado.util;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import fiado.exception.DaoException;


public class DbUtilOracle {

	private static final String URL_DATABASE = "jdbc:oracle:thin:@localhost:1521";
	private static final String DRIVER_JDBC = "oracle.jdbc.driver.OracleDriver";
	
	private static final String USUARIO_DB = "professornpc";
	private static final String SENHA_USUARIO_DB = "prof123";
//	private static final String USUARIO_DB = "prova";
//	private static final String SENHA_USUARIO_DB = "prova123";
	
	static {
		try {
			Class.forName(DRIVER_JDBC);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection() throws DaoException {
		try {
			Connection connection = DriverManager.getConnection(URL_DATABASE, USUARIO_DB, SENHA_USUARIO_DB);
			connection.setAutoCommit(true);
			return connection;
		} catch (SQLException e) {
			throw new DaoException(e);
		}
	}
	
	public static java.util.Date getJavaDate(ResultSet result, String nomeCampo) throws SQLException {
		Date dataFalecimento = result.getDate(nomeCampo);
		java.util.Date javaDate = null;
		if (dataFalecimento != null) {
			javaDate = new Date(dataFalecimento.getTime());
		}
		return javaDate;
	}
	
	public static Date getSqlDate(java.util.Date date) {
		Date sqlDate = null;
		if (date != null) {
			sqlDate = new java.sql.Date(date.getTime());
		}
		return sqlDate;
	}
	
	public static void close(Connection conn, Statement statement, ResultSet result) throws DaoException {
		try {
			if (conn != null) {
				conn.close();
			}
			if (statement != null) {
				statement.close();
			}
			if (result != null) {
				result.close();
			}
		} catch (SQLException e) {
			throw new DaoException(e);
		}

	}
}
